﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using nanoCMS.Blog.Concrete;
using nanoCMS.Blog.Providers;
using nanoCMS.Core;

namespace nanoCMS.Blog.WebUI.Models {
	public class BlogCategoryService
	{

		private readonly WorldContext worldContext;
		private BlogCategoryProvider blogCategoryProvider;


		public BlogCategoryService(WorldContext worldContext)
		{
			this.worldContext = worldContext;
			blogCategoryProvider = new BlogCategoryProvider(worldContext);
		}

		public List<BlogCategoryVM> GetBlogCategoriesForTenant(int tenantID)
		{
			return blogCategoryProvider.GetCategoriesForTenant(tenantID).Select(BlogCategoryVMFactory).ToList();
		}

		public BlogCategoryVM GetBlogCategory(int blogCategoryID)
		{
			var blogCategory = blogCategoryProvider.Get(blogCategoryID);
			if (blogCategory == null) throw  new Exception();
			return BlogCategoryVMFactory(blogCategory);
		}

		public bool EditBlogCategory(BlogCategoryVM model)
		{
			return blogCategoryProvider.Edit(model.BlogCategoryID, model.Title, model.Description);
		}

		public void AddBlogCategory(BlogCategoryVM model)
		{
			BlogCategory blogCategory;
			blogCategoryProvider.Add(model.Title, model.Description, out blogCategory, WorldContext.TenantID);

		}

		#region factories
		private static BlogCategoryVM BlogCategoryVMFactory(BlogCategory blogCategory)
		{
			return new BlogCategoryVM
				   {
					BlogCategoryID = blogCategory.BlogCategoryID,
					Title = blogCategory.Title,
					Description = blogCategory.Description,
					TenantID = blogCategory.TenantID
				   };
		}

		#endregion

	}
}
